﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.DLBK;
using StateGrid.User;
using StateGrid.GlobalVariables;
using iTextSharp.text.pdf;
using StateGrid.OperationLog;

public partial class AddPM :  System.Web.UI.Page
{
    /// <summary>
    /// 电力报刊操作对象
    /// </summary>
    private DLBK_DAO dlbkDAO = new DLBK_DAO();
    /// <summary>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }

        if (!this.IsPostBack)
        {
            this.BindData();
        }

    }

    /// <summary>
    /// 绑定初始化数据
    /// </summary>
    private void BindData()
    {
        String message = null; 

        String flbm = HttpUtility.UrlDecode(Request["flbm"]);
        if (null == flbm || "".Equals(flbm))
        {
            message = "错误：分类编码错误";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }
       

        //获取对应的分类信息
        DLBK_FL dlbkFL = this.dlbkDAO.GetDLBK_FLByFlbm(flbm);

        if (null == dlbkFL)
        {
            message = "错误：没有找到对应的分类编码";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }


        this.backHyperLink.NavigateUrl = GlobalDLBK.LISTPM_PAGE + "?catalog=" + dlbkFL.flbm;

        //分类编码
        this.flbmTextBox.Text = dlbkFL.flbm;
        //分类名称
        this.flmcTextBox.Text = dlbkFL.flmc;

        //标题
        this.btTextBox.Text = "";

        //报告日期
        this.rqTextBox.Text = "";

        //栏目
        this.lmTextBox.Text = "";
        //作者
        this.zzTextBox.Text = "";

        //版号
        this.bhTextBox.Text = "";
        //正文
        this.zwTextBox.Text = "";

        User user = (User)Session["login"];
        if (null == user)
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }
        //登录人
        this.dlrTextBox.Text =user.name;
    }

    /// <summary>
    /// 获取用户填写的电力报刊篇名信息
    /// </summary>
    /// <returns></returns>
    private DLBK_PM GetDLBKPM()
    {
        DLBK_PM dlbkPM = new DLBK_PM();
        //分类编码
        dlbkPM.flbm = this.flbmTextBox.Text;
        //标题
        dlbkPM.bt = this.btTextBox.Text.Trim();
        //作者
        dlbkPM.zz = this.zzTextBox.Text.Trim();
        //日期
        dlbkPM.rq = DateTime.Parse(this.rqTextBox.Text);
        //版号
        dlbkPM.bh = Int32.Parse(this.bhTextBox.Text);
        //栏目
        dlbkPM.lm = this.lmTextBox.Text.Trim();
        //更新日期
        dlbkPM.gxrq = DateTime.Parse(this.rqTextBox.Text);
        //登录人
        dlbkPM.dlr = this.dlrTextBox.Text;
        //正文
        dlbkPM.zw = this.zwTextBox.Text;

        return dlbkPM;

    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
      
        if (!this.ValidateRQ())
        {
            return;
        }
        if (!this.ValidateBT())
        {
            return;
        }
        if (!this.ValidateBH())
        {
            return;
        }
      
        DLBK_PM dlbkPM = this.GetDLBKPM();

        if (this.dlbkDAO.InsertDLBK_PM(dlbkPM))
        {
            Response.Write("<Script>alert('添加电力报刊篇名信息成功')</Script>");
            this.BindData();
        }
        else
        {
            Response.Write("<Script>alert('添加电力报刊篇名信息失败')</Script>");
        }
    }



    /// <summary>
    /// 验证日期
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateRQ()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        this.rqTextBox.Text = this.rqTextBox.Text.Trim();
        try
        {
            DateTime.Parse(this.rqTextBox.Text.Trim());
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：报告日期不正确";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证标题
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateBT()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        if (null == this.btTextBox.Text || "".Equals(this.btTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误：标题不能为空";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证版号
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateBH()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        this.bhTextBox.Text = this.bhTextBox.Text.Trim();
        if (null == this.bhTextBox.Text || "".Equals(this.bhTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误：版号不能为空";
            flag = false;
        }

        try
        {
            Int32.Parse(this.bhTextBox.Text);
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：版号请输入数字";
            flag = false;
        }
        return flag;
    }

    protected void zzTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zzTextBox.Text = GlobalMethod.Semicolon(this.zzTextBox.Text.Trim());
    }

    protected void resetButton_Click(object sender, EventArgs e)
    {
        this.BindData();
    }
}